import 'package:flutter/material.dart';
import 'package:flutter_mall_app/model/CartInfoModel.dart';
import 'package:flutter_mall_app/pages/shopping_cart/cart_item.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../provide/shopping_cart.dart';
import 'package:provide/provide.dart';
import 'cart_bottom.dart';

class ShoppingCart extends StatefulWidget {
  @override
  _ShoppingCartState createState() => _ShoppingCartState();
}

class _ShoppingCartState extends State<ShoppingCart> {
  Future<String> _getCartInfo() async {
    await Provide.value<ShoppingCartProvide>(context).getCartInfo();
    return "end";
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("购物车"),
      ),
      body: FutureBuilder(
        future: _getCartInfo(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            List<CartInfoModel> list = Provide.value<ShoppingCartProvide>(context).cartInfoList;
            return Stack(
              children: <Widget>[
                Provide<ShoppingCartProvide>(
                  builder: (context, child, scoped) {
                    list = Provide.value<ShoppingCartProvide>(context).cartInfoList;
                    return ListView.builder(
                      itemCount: list.length,
                      itemBuilder: (context, index) {
                        return CartItem(item: list[index]);
                      },
                    );
                  },
                ),
                Positioned(
                  child: CartBottom(),
                  bottom: 0,
                  left: 0,
                  right: 0,
                ),
              ],
            );
          } else {
            return Center(
              child: SpinKitFadingCircle(
                itemBuilder: (_, int index) {
                  return DecoratedBox(
                    decoration: BoxDecoration(
                      color: index.isEven ? Colors.red : Colors.green,
                    ),
                  );
                },
              ),
            );
          }
        },
      ),
    );
  }
}
